HS.createNS('HS.Web');
HS.Web.map = new function () {

    var default_lat = 29.7018139;
    var default_lng = -95.57736620000003;

    this.init = function () {
        google.maps.event.addDomListener(window, 'load', initialize);

        $("#lblSearch").click(function () {
            var $address = $("#txtAddress").val().trim();
            if ($address == '') {
                alert('Vui lòng nhập địa chỉ');
            }
            getCoordinates($address);

        });

        function getCoordinates(address) {
            geocoder.geocode({ 'address': address}, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var $lng = results[0].geometry.location['D'];
                    var $lat = results[0].geometry.location['k'];

                    var pos = new google.maps.LatLng($lat, $lng);

                    var mapOptions = {
                        content: 'You are here',
                        position: pos,
                        map: map,
                        zoom: 15
                    };
                    var map = new google.maps.Map(document.getElementById('map-canvas'),
                        mapOptions);

                    map.setCenter(pos);

                    createMarker('You are here', $lat, $lng, map);
                    google.maps.event.addDomListener(window, 'load', initialize);

                } else {
                    return {'error': 'Không tìm được địa chỉ bạn yêu cầu.'}
                }
            }, function () {
                return {'error': 'Không tìm được địa chỉ bạn yêu cầu.'}
            });
        }

        function geoFindMe() {
            if (!navigator.geolocation) {
                alert("Geolocation is not supported by your browser");
                return;
            }

            function success(position) {

                var latitude = position.coords.latitude;
                var longitude = position.coords.longitude;
                var pos = new google.maps.LatLng(position.coords.latitude,
                    position.coords.longitude);

                var mapOptions = {
                    content: 'Location found using HTML5.',
                    position: pos,
                    map: map,
                    zoom: 15
                };
                var map = new google.maps.Map(document.getElementById('map-canvas'),
                    mapOptions);

                map.setCenter(pos);

                createMarker('You are here', latitude, longitude, map);
                google.maps.event.addDomListener(window, 'load', initialize);

            };

            navigator.geolocation.getCurrentPosition(success, error);
        }

        function createMarker(add, lat, lng, map) {
            var contentString = add;
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(lat, lng),
                map: map,
                content: 'You are here'
            });

            google.maps.event.addListener(marker, 'click', function () {
                var infowindow = new google.maps.InfoWindow();
                infowindow.setContent(contentString);
                infowindow.open(map, marker);
            });
            bounds.extend(marker.position);

        }

        function createInfoWindow(add, lat, lng, map) {
            var contentString = add;
            var infowindow = new google.maps.InfoWindow({
                position: new google.maps.LatLng(lat, lng),
                map: map
            });

            google.maps.event.addListener(marker, 'click', function () {
                infowindow.setContent(contentString);
                infowindow.open(map, marker);
            });

            bounds.extend(marker.position);

        }

        function error() {
            alert("Unable to retrieve your location");
        }

        function initialize() {

            var mapOptions = {
                center: { lat: default_lat, lng: default_lng},
                zoom: 10,
                position: new google.maps.LatLng(default_lat, default_lng)
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);
        }
    }

}